package com.orangelife.mobile.myhouse.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.myhouse.activity.HouseWaitConfirmActivity;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.HouseBean;
import com.orangelife.mobile.myhouse.entity.WaitCertEntity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 待认证ITEM
 * <p/>
 * Created by 吴格非 on 2015/11/12.
 */
public class HouseWaitConfirmLvAdapter extends FatherAdapter {

    private Context mContext;

    private List<WaitCertEntity> mList = new ArrayList();

    private String mCurrentStatus = HouseBean.STATUS_WAIT;

    public HouseWaitConfirmLvAdapter(Context context) {
        this.mContext = context;
    }

    /**
     * 设置当前列表的状态，待认证或者已过期
     */
    public void setCurrentStatus(String currentStatus) {
        this.mCurrentStatus = currentStatus;
    }

    @Override
    public BaseAdapter setList(List list) {
        this.mList = list;
        return this;
    }

    @Override
    public int getCount() {
        if (mList == null) {
            return 0;
        }
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_wait_confirm, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvHouseInfo.setTag("" + position);
        holder.tvHouseInfo.setOnLongClickListener(longClickListener);
        holder.tvHouseInfo.setText(mList.get(position).getHouseInfo());
        holder.tvIdentity.setText(HouseBiz.getIdentityName(mContext, mList.get(position).getType()));
        holder.ivJumpCode.setTag(mList.get(position).getApplicationID());
        holder.tvIdentity.setTag(mList.get(position).getApplicationID());
        holder.tvIdentity.setOnClickListener(listener);
        holder.ivJumpCode.setOnClickListener(listener);
        return convertView;
    }

    View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (mCurrentStatus.equals(HouseBean.STATUS_EXPIRE)) {
                showWarningDialog(v);
            }
            return false;
        }
    };

    View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String certID = String.valueOf(v.getTag());
            jumpToDetail(certID);
        }
    };

    /**
     * 之所以要抽成方法，因为这是这个类的一个主要功能，抽成方法，虽然多写了几行代码，但是提高了代码可读性
     */
    private void jumpToDetail(String certID) {
        if (mCurrentStatus.equals(HouseBean.STATUS_WAIT)) {
            HouseJumper.jumpToHouseCode(mContext, certID);
        } else {//STATUS_EXPIRE 已过期
            HouseJumper.jumpToCodeExpire(mContext, certID);
        }
    }

    private void showWarningDialog(final View v) {
        final ConfirmDialog dialog = new ConfirmDialog(mContext, "提示", "确定要删除吗",
                "取消", "确认");
        dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog.dismiss();
            }

            @Override
            public void doRight() {
                delItem(v);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void delItem(View v) {
        final int pos = Integer.parseInt(String.valueOf(v.getTag()));
        if (!StringUtil.validateList(mList, pos)) {
            return;
        }
        String applicationID = mList.get(pos).getApplicationID();
        ((HouseWaitConfirmActivity) mContext).showDialog((Activity) mContext);
        HouseHttpBiz.delExpireUser(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                ((HouseWaitConfirmActivity) mContext).closeDialog();
                boolean isOk = OrangeErrorHandler.getInstance().getInstance().isSuccess(response, "对不起，删除失败，请重试", "删除成功");
                if (isOk && StringUtil.validateList(mList, pos)) {
                    mList.remove(pos);
                    notifyDataSetChanged();
                }
            }
        }, applicationID);
    }

    static class ViewHolder {
        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        @Bind(R.id.rlRoot)
        RelativeLayout rlRoot;
        @Bind(R.id.tvHouseInfo)
        TextView tvHouseInfo;
        @Bind(R.id.tvIdentity)
        TextView tvIdentity;
        @Bind(R.id.ivJumpCode)
        ImageView ivJumpCode;
    }

}
